package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.FundEntity;
import com.hdjk.view.util.StringAppUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sxy on 2017/11/2.
 */

public class FundListAdapter extends BaseAdapter {
    private List<FundEntity> mList;
    private Context mContext;

    public FundListAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<FundEntity> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public FundEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_fund_list, null);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.tv_profit = (TextView) convertView.findViewById(R.id.tv_profit);
            holder.tv_earning = (TextView) convertView.findViewById(R.id.tv_earning);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        FundEntity entity = mList.get(position);
        holder.tv_name.setText(entity.getFundName());
        holder.tv_type.setText(entity.getFundCode() + "  " + entity.getFundTypeName());
        holder.tv_profit.setText("近一年收益");
//        if (entity.getFundType().equals("MM")) {
//            holder.tv_earning.setText(StringAppUtil.formatDecimal(entity.getSevenDaysAnnualizedYield(), 4) + "%");
//        } else {
            String earnings = "";
            if (null != entity.getYear1() && !entity.getYear1().equals("")) {
                earnings = StringAppUtil.formatDecimal(new BigDecimal(entity.getYear1()).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(), 2) + "%";
            } else {
                earnings = "暂无";
            }
            holder.tv_earning.setText(earnings);
//        }
        return convertView;
    }

    class Holder {
        TextView tv_name, tv_type, tv_earning, tv_profit;
    }
}
